package com.minelx.statementsplus.split;

import com.minelx.statementsplus.MajorSql;
import com.minelx.statementsplus.common.Splitter;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

import static com.minelx.statementsplus.split.LongArraySplitter.partitionsCount;
import static java.util.stream.Collectors.toList;

public class StringArraySplitter implements MajorSplitter {
	private static final int FIX_PARTITION_LENGTH = 1000;

	private final String[] items;

	private final Function<String[], MajorSql> mapper;

	public StringArraySplitter(String[] items, Function<String[], MajorSql> mapper) {
		this.items = items;
		this.mapper = mapper;
	}

	@Override
	public List<MajorSql> split() {
		return Splitter.splitting(Arrays.asList(items), partitionsCount(items.length, FIX_PARTITION_LENGTH)).stream()
				.map(partition -> partition.toArray(new String[0]))
				.map(mapper)
				.collect(toList());
	}
}
